package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.login.AuthLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "充值管理")
@RestController
@RequestMapping("api/account")
@SuppressWarnings({"all"})
public class RechargeInfoApiController {

    @Autowired
    private RechargeInfoService rechargeInfoService;

    @AuthLogin
    @Operation(summary = "根据充值订单编号查询充值信息")
    @GetMapping("/rechargeInfo/getRechargeInfo/{orderNo}")
    public Result<RechargeInfo> getRechargeInfo(@PathVariable String orderNo) {
        RechargeInfo rechargeInfo = rechargeInfoService.getOne(new LambdaQueryWrapper<RechargeInfo>()
                .eq(RechargeInfo::getOrderNo, orderNo));
        return Result.ok(rechargeInfo);
    }

    /**
     * 调用微信接口支付成功,修改充值订单状态
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "微信支付成功后，处理充值业务")
    @GetMapping("/rechargeInfo/rechargePaySuccess/{orderNo}")
    public Result rechargePaySuccess(@PathVariable String orderNo) {
        rechargeInfoService.rechargePaySuccess(orderNo);
        return Result.ok();
    }

    /**
     * 获取用户充值记录
     *
     * @param page
     * @param limit
     * @return
     */
    @AuthLogin
    @Operation(summary = "查询用户充值记录")
    @GetMapping("/userAccount/findUserRechargePage/{page}/{limit}")
    public Result<Page<UserAccountDetail>> findUserRechargePage(@PathVariable Long page, @PathVariable Long limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<UserAccountDetail> userAccountDetailPage = new Page<>(page, limit);
        String tradeType = SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT;
        userAccountDetailPage = rechargeInfoService.findUserRechargePage(userAccountDetailPage, userId,tradeType);
        return Result.ok(userAccountDetailPage);
    }

    /**
     * 获取用户消费记录
     *
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "查询用户消费记录")
    @GetMapping("/userAccount/findUserConsumePage/{page}/{limit}")
    public Result<Page<UserAccountDetail>> findUserConsumePage(@PathVariable Long page, @PathVariable Long limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<UserAccountDetail> userAccountDetailPage = new Page<>(page, limit);
        String tradeType = SystemConstant.ACCOUNT_TRADE_TYPE_MINUS;
        userAccountDetailPage = rechargeInfoService.findUserRechargePage(userAccountDetailPage, userId,tradeType);
        return Result.ok(userAccountDetailPage);
    }
}

